package cn.iocoder.yudao.module.system.service.imsuser;

//import cn.iocoder.ny.module.mall.domain.SystemUsers;
//import cn.iocoder.ny.module.mall.service.impl.SystemUserServiceImpl;
import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.ImsUsersVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.SystemUserVo;
import cn.iocoder.yudao.module.system.dal.dataobject.activity.ActivityUser;
import cn.iocoder.yudao.module.system.dal.dataobject.saas.dto.StaffBindDto;
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.imsuser.ImsUsers;
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.system.SystemUsers;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.imsuser.ImsUsersMapper;
import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMapper;
import cn.iocoder.yudao.module.system.dal.mysql.system.SystemUsersMapper;
import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
import cn.iocoder.yudao.module.system.service.activity.ActivityService;
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.system.SystemUserServiceImpl;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author luchunhong
 * @date 2024/10/28/ 10:53
 * @description:
 */
@Service
@DS("slave")
public class ImsUserServiceImpl extends ServiceImpl<ImsUsersMapper, ImsUsers> implements ImsUserService {

    @Autowired
    private OAuth2TokenService oAuth2TokenService;
    @Autowired
    private AdminUserService adminUserService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SystemUserServiceImpl systemUserService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private SystemUsersMapper systemUsersMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private ActivityService activityService;

    @Override
//    @Transactional
    public Boolean loginByImsUsers(ImsUsersVO imsUsersVO) {
        if (!imsUsersVO.getPassword().equals(imsUsersVO.getPasswordAgain())){
            throw new RuntimeException("两次密码不一致");
        }
        // 查询活动用户:主要查询的是ims_user这张表 查询这张表内有没有该活动用户
        ImsUsers imsUsers = lambdaQuery().eq(ImsUsers::getUsername, imsUsersVO.getUsername())
                .one();
        if (ObjectUtils.isEmpty(imsUsers)){
            return null;
        }
        // 查询systemUser表内有没有该数据,查询在我们聪明掌柜总后台里面的system_user表内有没有该用户存在
        List<SystemUserVo> systemUserByUserName = systemUsersMapper.getSystemUserByUserName(imsUsersVO.getUsername());
        // 若system_user表内没有该用户
        if (ObjectUtils.isEmpty(systemUserByUserName)){
            SystemUsers systemUsers = new SystemUsers();
            systemUsers.setUsername(imsUsersVO.getUsername());
            systemUsers.setNickname(imsUsersVO.getUsername());
            systemUsers.setMobile(imsUsersVO.getUsername());
            systemUsers.setPassword(passwordEncoder.encode(imsUsersVO.getPassword()));
            // 默认在1号店铺
            systemUsers.setTenantId(1L);
            // 设置为Date
            long endTime = imsUsers.getEndtime() * 1000L;
            Date expireDate = new Date(endTime);
            systemUsers.setExpireTime(expireDate);  // 执行双插
            systemUserService.save(systemUsers);
            ActivityUser activityUser = new ActivityUser();
            activityUser.setUserId(systemUsers.getId());
            activityUser.setRole("imsUser");
            activityUser.setActivityId(0L);
            activityUser.setTenantId(systemUsers.getTenantId());
            activityService.save(activityUser);
            // 设置该用户为活动用户权限
            StaffBindDto staffBindDto = new StaffBindDto();
            // 设置该用户为活动用户
            RoleDO roleDO = roleMapper.selectByCodeOrTenantId("activity", 1L);
            staffBindDto.setRoleIds(Collections.singleton(roleDO.getId()));
            permissionService.assignUserRole(systemUsers.getId(), staffBindDto.getRoleIds());
            return true;
        }
        return  null;
    }

    @Override
    public Boolean queryImsUser(ImsUsersVO imsUsersVO) {
        ImsUsers one = lambdaQuery().eq(ImsUsers::getUsername, imsUsersVO.getUsername()).one();
        if (ObjectUtils.isEmpty(one)){
            return false;
        }
        // 查询systemUser表内有没有该数据
        List<SystemUserVo> systemUserByUserName = systemUsersMapper.getSystemUserByUserName(imsUsersVO.getUsername());
        // 若system_user表内没有该用户
        if (ObjectUtils.isEmpty(systemUserByUserName)){
          // 提示需要重新绑定密码
            return true;
        }
        return  false;
    }

    private UserTypeEnum getUserType() {
        return UserTypeEnum.ADMIN;
    }
}
